package com.example.animation0415;

import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

//帧动画
public class FrameActivity extends AppCompatActivity implements View.OnClickListener{
    private Button start;
    private Button stop;
    private ImageView img_view;
    private AnimationDrawable animationDrawable;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.framelayout);
        initView();
    }

    private void initView() {
        start = findViewById(R.id.start);
        stop = findViewById(R.id.stop);
        img_view = findViewById(R.id.img_view);

        start.setOnClickListener(this);
        stop.setOnClickListener(this);

        //通过逐帧动画的资源文件过得AnimationDrawable示例
        animationDrawable = (AnimationDrawable) getResources().getDrawable(R.drawable.frame_animation);
        //把AnimationDrawable设置为ImageView的背景
        img_view.setBackgroundDrawable(animationDrawable);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.start:
                start();
                break;
            case R.id.stop:
                stop();
                break;

        }
    }

    protected void start(){
        if(animationDrawable != null && !animationDrawable.isRunning()){
            animationDrawable.start();
        }
    }

    protected void stop(){
        if(animationDrawable != null && animationDrawable.isRunning()){
            animationDrawable.stop();
        }
    }
}
